package com.gravata.enigma.model;

import java.io.Serializable;
import java.sql.SQLException;

import com.gravata.enigma.Utils;
import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName="bouquet")
public class Bouquet  extends EntidadeAbstrata implements  Comparable<Bouquet> {

	private static final long serialVersionUID = 6627017749183354815L;

	@DatabaseField(generatedId=true)
	private int id;

	@DatabaseField(canBeNull=false,columnName="reference")
	private String reference;

	@DatabaseField(canBeNull=false,columnName="name")
	private String name;

	@DatabaseField(canBeNull=false,columnName="ordem",unique=false)
	private int ordem;

	@DatabaseField(canBeNull=false,foreign=true,foreignAutoRefresh=true)
	private Profile profile;

	@ForeignCollectionField(eager=false,orderAscending=true,orderColumnName="ordem")
	private ForeignCollection<BouquetService> services;

	public String getReference() {
		if(reference==null){
			reference="#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET \"userbouquet."+Utils.removeAcentosEspacos(name.toLowerCase())+".tv\" ORDER BY bouquet";
		}
		return reference;
	}

	public String getBouquetFile(){
		String file="#NAME "+getName()+System.getProperty("line.separator");

		for(BouquetService service:getServices()){
			String ref=service.getService().getReference();
			ref=ref.substring(0, ref.lastIndexOf(":"))+":";
			file+=ref+service.getService().getName()+System.getProperty("line.separator");
		}

		return file;
	}

	public String getUserBouquetTv(){
		return Utils.getUserBouquet(getReference());
	}

	public boolean isFavorites(){
		return getReference().contains(".favourites.");
	}

	public void setReference(String reference) {
		this.reference = reference;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public int getOrdem() {
		return ordem;
	}
	public void setOrdem(int ordem) {
		this.ordem = ordem;
	}

	public ForeignCollection<BouquetService> getServices() {
		return services;
	}

	public Profile getProfile() {
		return profile;
	}
	public void setProfile(Profile profile) {
		this.profile = profile;
	}

	public BouquetService getService(int indice){
		int i=0;

		for(BouquetService bouquetService:getServices()){
			if(i==indice)
				return bouquetService;
			i++;
		}
		return null;
	}

	public void reOrdenar(int from, int to){
		BouquetService bFrom=getService(from);
		BouquetService bTo=getService(to);

		bFrom.setOrdem(to+1);
		bTo.setOrdem(from+1);

		try {
			getServices().update(bFrom);
			getServices().update(bTo);

		} catch (SQLException e) {}
	}

	public Bouquet() {
	}

	@Override
	public int compareTo(Bouquet another) {
		if(ordem==another.getOrdem())
			return 0;
		else if(ordem<another.getOrdem())
			return -1;
		else
			return 1;
	}

	@Override
	public Serializable getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
}